package com.erdos.message;

import java.io.Serializable;

import javax.jms.Message;

import org.apache.commons.lang.builder.ToStringBuilder;

/**
 * this is for encapsulating the sending/receiving data.
 * @author sunf
 *
 */
public class GenericMessage implements Serializable {
	private static final long serialVersionUID = 8351576549341739340L;
	public static final String MSG_TYPE_ID_KEY="MSG_TYPE_ID_KEY";
	public static final String MSG_ID_KEY="MSG_ID_KEY";
	/**
	 * a unique identifier for this message
	 */
	private String messageId;
	
	/**
	 * a unique identifier generated by JMS server.
	 */
	private String jmsMessageId;
	
	/**
	 * message header holds the transfer information
	 */
	private MessageHeader messageHeader;
	/**
	 * message body to be transferred
	 */
	private Object messageBody;
	
	/**
	 * target Jms Message to be converted into
	 */
	private Message targetMessage;
	/**
	 * to holds the Exception of message sending/receiving/processing
	 */
	private Exception exception;

	public GenericMessage() { 
		this.messageHeader=new MessageHeader();
	}
	
	public GenericMessage(MessageHeader messageHeader, Serializable messageBody) {
		this.messageHeader=messageHeader;
		this.messageBody=messageBody;
	}
	/**
	 * Getter of the property <tt>messageHeader</tt>
	 * 
	 * @return Returns the messageHeader.
	 * 
	 */
	public MessageHeader getMessageHeader() {
		return messageHeader;
	}

	/**
	 * Setter of the property <tt>messageHeader</tt>
	 * 
	 * @param messageHeader
	 *            The messageHeader to set.
	 * 
	 */
	public void setMessageHeader(MessageHeader messageHeader) {
		this.messageHeader = messageHeader;
	}

	/**
	 * supposed the returned message body object is serializable or 
	 * with serializable elements in a Map or Collection or array.
	 * @return message body object
	 */
	public Object getMessageBody() {
		return messageBody;
	}

	/**
	 * sets the message body to encapsulate what to deliver.
	 * @param messageBody messageBody should be serializable, or for Map and Collection, array
	 * the elements should be serializable
	 */
	public void setMessageBody(Object messageBody) {
		this.messageBody = messageBody;		
	}	
	
	/**
	 * @return the messageId
	 */
	public String getMessageId() {
		return messageId;
	}

	/**
	 * @param messageId the messageId to set
	 */
	public void setMessageId(String messageId) {
		this.messageId = messageId;
	}	
	
	/**
	 * @return the targetMessage
	 */
	public Message getTargetMessage() {
		return targetMessage;
	}

	/**
	 * if need to convert this instance into the target Jms Message,
	 * need to call this method.
	 * @param targetMessage the targetMessage to set
	 */
	public void setTargetMessage(Message targetMessage) {
		this.targetMessage = targetMessage;
	}

	/**
	 * 
	 * @return the message type Id
	 */
	public String getMessageTypeId() {
		return this.messageHeader.getMessageTypeId();
	}
	
	public void setMessageTypeId(String msgTypeId) {
		this.messageHeader.setMessageTypeId(msgTypeId);
	}
	
	protected Class<?> getBodyType() {
		String className= this.messageHeader.getBodyType();
		if(className==null) {
			return null;
		}
		Class<?> clz=null;
		try {
			clz=Class.forName(className);			
		} catch (ClassNotFoundException e) {			
			e.printStackTrace();
		}
		return clz;
	}

	public String getJmsMessageId() {
		return jmsMessageId;
	}

	public void setJmsMessageId(String jmsMessageId) {
		this.jmsMessageId = jmsMessageId;
	}

	public Exception getException() {
		return exception;
	}

	public void setException(Exception exception) {
		this.exception = exception;
	}

	@Override
	public String toString() {		
		return new ToStringBuilder(this).append(this.messageHeader).append(this.messageBody).toString();
	}	
	
}
